package com.bw.repair.view

import android.app.AlertDialog
import android.content.DialogInterface
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.repair.adapter.RepairAdapter
import com.bw.repair.databinding.ActivityRepairBinding
import com.bw.repair.entity.RepairEntity
import com.bw.repair.net.RepairViewModel


/**
 * 智慧园区-06-物业报修V1.1
 * 维修管理
 * 兰宏宇
 * 20250511
 */
@Route(path = RoutePath.PATH_REPAIR)
class RepairActivity : BaseMvvmActivity<ActivityRepairBinding, RepairViewModel>() {

    private lateinit var adapter: RepairAdapter
    private var allRepairList = mutableListOf<RepairEntity>()
    private lateinit var dialog: AlertDialog
    private var deletePosition = -1
    override fun initView() {

        binding.bar.ivBack.setOnClickListener { finish() }
        binding.bar.tvTitle.visibility = View.GONE
        binding.bar.tvLeftTitle.visibility = View.VISIBLE
        binding.bar.tvLeftTitle.text = "维修管理"

        adapter = RepairAdapter()
        binding.rvRepair.adapter = adapter
        binding.rvRepair.layoutManager = LinearLayoutManager(this)

        binding.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (allRepairList.size > 0) {
                    searchMethod(s.toString())
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        binding.llFbtn.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_ADD_REPAIR)
                .withSerializable("repairEntity", RepairEntity()).navigation()
        }

        adapter.setOnItemClickListener { aAdapter, view, position ->
            val entity = adapter.data[position]
            ARouter.getInstance().build(RoutePath.PATH_ADD_REPAIR)
                .withSerializable("repairEntity", entity).navigation()
        }

        binding.llPai.setOnClickListener {
            adapter.data.reverse()
            adapter.notifyDataSetChanged()
        }

        adapter.setOnItemLongClickListener { aAdapter, view, position ->
            val repairEntity = adapter.data[position]

            dialog = AlertDialog.Builder(this)
                .setNegativeButton(
                    "确定"
                ) { dialog, which ->
                    deletePosition = position
                    vm.deleteRepair(repairEntity.id)
                    dialog.dismiss()

                }.setPositiveButton("取消") { dialog, which ->
                    dialog.dismiss()

                }
                .setTitle("确定删除吗")
                .create()


            dialog.show()

            true
        }



    }

    override fun initData() {
        vm.repairSuccess.observe(this) {
            adapter.data.clear()
            allRepairList.clear()
            adapter.data.addAll(it)
            allRepairList.addAll(it)
            adapter.notifyDataSetChanged()

        }

        vm.deleteRepairSuccess.observe(this){
            if(deletePosition!=-1){
                adapter.notifyItemRemoved(deletePosition)
                adapter.data.removeAt(deletePosition)
                ToastUtils.showShort("删除成功")
                deletePosition = -1
            }

        }

    }

    override fun onDestroy() {
        super.onDestroy()

    }

    override fun onResume() {
        super.onResume()
        vm.getRepair()
    }

    fun searchMethod(searchStr: String) {

        val filter = allRepairList.filter { repairEntity ->
            repairEntity.repairName.contains(searchStr) ||
                    repairEntity.orderNumber.contains(searchStr) ||
                    repairEntity.contactMethod.contains(searchStr) ||
                    repairEntity.faultDescription.contains(searchStr) ||
                    repairEntity.processingStatus.contains(searchStr)
        }

        adapter.data.clear()
        adapter.data.addAll(filter)
        adapter.notifyDataSetChanged()
    }


}